<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<script type="text/javascript">
    require([ "dojo/_base/array", "dojo/_base/xhr", "dojo/request", "dojo/topic", "dojo/json" ],
            function(array, xhr, request, topic, JSON) {
                var deletItemsCount = 0;
                mailboxRestForm = function(formId) {
                    dijit.byId(formId).reset();
                    if (formId.indexOf('UFD') != -1) {
                        var rowid = jQuery("#mailboxGird").jqGrid('getGridParam', 'selrow');
                        var mailbox = jQuery("#mailboxGird").jqGrid('getRowData', rowid);
                        dijit.byId('mailboxUFD').setValues(mailbox);
                    }
                };
                topic.subscribe("mailbox/insertOrUpdate", mailboxRestForm);
                var syncDB = function(url, jsonData) {
                    request.post(url, {
                        data : jsonData,
                        headers : {
                            'Content-Type' : "application/json; charset=utf-8"
                        },
                        timeout : 30000
                    }).then(function(response) {
                        topic.publish("notifyMsgTopic", {
                            message : response,
                            type : "message",
                            duration : 1000
                        });
                        var currentPage = jQuery('#mailboxGird').jqGrid('getGridParam', 'page');
                        var pageSize = jQuery('#mailboxGird').jqGrid('getGridParam', 'rowNum');
                        var totalCount = jQuery('#mailboxGird').jqGrid('getGridParam', 'records') - deletItemsCount;
                        var totalPages = Math.ceil(totalCount / pageSize);
                        jQuery("#mailboxGird").jqGrid('setGridParam', {
                            page : currentPage > totalPages ? totalPages : currentPage
                        }).trigger("reloadGrid");
                    }, function(error) {
                        alert(error);
                    });
                };
                mailboxSubmitForm = function(formId, type) {
                    var url = "mailbox/insert";
                    if (type == 'u') {
                        url = "mailbox/update";
                    }
                    var form = dijit.byId(formId);
                    if (form.isValid()) {
                        var json = JSON.stringify(form.getValues(), true);
                        syncDB(url, json);
                        form.hide();
                        topic.publish("mailbox/insertOrUpdate", formId);
                    } else {
                        form.validate();
                    }
                };
                mailboxDelete = function() {
                    var ids = jQuery("#mailboxGird").jqGrid('getGridParam', 'selarrrow') || [];
                    deletItemsCount = ids.length;
                    if (ids.length > 0) {
                        if (confirm('确认删除所选定的项？')) {
                            var mailbox = {};
                            mailbox.id = '';
                            array.forEach(ids, function(entry, i) {
                                mailbox.id += entry + ",";
                            });
                            syncDB("mailbox/delete", JSON.stringify(mailbox, true));
                        }
                    } else {
                        alert('请选择至少一条数据!');
                    }
                };
                mailboxQuery = function(formId) {
                    var mailbox = dijit.byId(formId).getValues();
                    jQuery("#mailboxGird").jqGrid('setGridParam', {
                        postData : mailbox,
                        mtype : "post"
                    }).trigger('reloadGrid');
                };
            });
</script>
<div data-dojo-type="dijit/Dialog" id="mailboxIFD" title="新增">
  <div class="dijitDialogPaneContentArea">
    <table>
      <tr>
        <td><label for="mailid">邮件ID: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="mailid"></td>
      </tr>
      <tr>
        <td><label for="folderid">收发类型：1收件箱、发件箱、草稿箱、垃圾箱: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="folderid"></td>
      </tr>
      <tr>
        <td><label for="userid">用户ID: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="userid"></td>
      </tr>
      <tr>
        <td><label for="sendtime">发送时间: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="sendtime"></td>
      </tr>
      <tr>
        <td><label for="delflag">del=1则代表删除: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="delflag"></td>
      </tr>
      <tr>
        <td><label for="readflag">查阅标识 1代表已查阅: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="readflag"></td>
      </tr>
      <tr>
        <td><label for="replyflag">回复标识 1代表已回复: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="replyflag"></td>
      </tr>
      <tr>
        <td><label for="note">说明: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="note"></td>
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="mailboxSubmitForm('mailboxIFD','i')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="mailboxRestForm('mailboxIFD');">重置</button>
  </div>
</div>
<div data-dojo-type="dijit/Dialog" id="mailboxUFD" title="更新">
  <div class="dijitDialogPaneContentArea">
    <input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, readOnly: true" type="text"
      name="boxid" style="display: none;">
    <table>
      <tr>
        <td><label for="mailid">邮件ID: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="mailid"></td>
      </tr>
      <tr>
        <td><label for="folderid">收发类型：1收件箱、发件箱、草稿箱、垃圾箱: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="folderid"></td>
      </tr>
      <tr>
        <td><label for="userid">用户ID: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="userid"></td>
      </tr>
      <tr>
        <td><label for="sendtime">发送时间: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="sendtime"></td>
      </tr>
      <tr>
        <td><label for="delflag">del=1则代表删除: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="delflag"></td>
      </tr>
      <tr>
        <td><label for="readflag">查阅标识 1代表已查阅: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="readflag"></td>
      </tr>
      <tr>
        <td><label for="replyflag">回复标识 1代表已回复: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="replyflag"></td>
      </tr>
      <tr>
        <td><label for="note">说明: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text"
          name="note"></td>
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="mailboxSubmitForm('mailboxUFD','u')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="mailboxRestForm('mailboxUFD');">重置</button>
  </div>
</div>
<fieldset>
  <legend>&nbsp;&nbsp;信息选项&nbsp;&nbsp;</legend>
  <form id="queryMailboxForm" data-dojo-type="dijit/form/Form" method="post">
    <div style="margin-left: 5px;">
      <table>
        <tr>
          <td><label for="mailid">邮件ID: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="mailid"></td>
        </tr>
        <tr>
          <td><label for="folderid">收发类型：1收件箱、发件箱、草稿箱、垃圾箱: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="folderid"></td>
        </tr>
        <tr>
          <td><label for="userid">用户ID: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="userid"></td>
        </tr>
        <tr>
          <td><label for="sendtime">发送时间: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="sendtime"></td>
        </tr>
        <tr>
          <td><label for="delflag">del=1则代表删除: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="delflag"></td>
        </tr>
        <tr>
          <td><label for="readflag">查阅标识 1代表已查阅: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="readflag"></td>
        </tr>
        <tr>
          <td><label for="replyflag">回复标识 1代表已回复: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="replyflag"></td>
        </tr>
        <tr>
          <td><label for="note">说明: </label></td>
          <td><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="note"></td>
        </tr>
      </table>
    </div>
  </form>
  <div style="padding: 5px;">
    <button id="mailboxInsertBtn" data-dojo-type="dijit/form/Button" type="submit" label="新增">
      <script type="dojo/method" event="onClick" args="evt">
        dijit.byId('mailboxIFD').show();
      </script>
    </button>
    <button id="mailboxDeleteBtn" data-dojo-type="dijit/form/Button" type="button" label="删除" onclick="mailboxDelete();"></button>
    <button id="mailboxUpdateBtn" data-dojo-type="dijit/form/Button" type="button" label="更新">
      <script type="dojo/method" event="onClick" args="evt">
        var ids = jQuery("#mailboxGird").jqGrid('getGridParam', 'selarrrow') || [];
        if (ids.length != 1) {
          alert('每次只能修改一条数据!');
        }else{
          var mailbox = jQuery("#mailboxGird").jqGrid('getRowData', ids[0]);
          dijit.byId('mailboxUFD').setValues(mailbox);
          dijit.byId('mailboxUFD').show();
        }
      </script>
    </button>
    <button id="mailboxQueryBtn" data-dojo-type="dijit/form/Button" type="button" label="查询"
      onclick="mailboxQuery('queryMailboxForm');"></button>
  </div>
</fieldset>
<div id="gird">
  <table id="mailboxGird"></table>
  <div id="mailboxToolbar"></div>
</div>
<script type="text/javascript">
    jQuery("#mailboxGird").jqGrid({
        url : 'mailbox/queryAsList',
        datatype : "json",
        autowidth : true,
        rownumbers : true,
        height : 'auto',
        colNames : [
          '收发箱ID',
          '邮件ID',
          '收发类型：1收件箱、发件箱、草稿箱、垃圾箱',
          '用户ID',
          '发送时间',
          'del=1则代表删除',
          '查阅标识 1代表已查阅',
          '回复标识 1代表已回复',
          '说明'
        ],
        colModel : [ 
        {
          name : 'boxid',
          index : 'boxid',
          width : 60,
          key : true,
          hidden : true,
          align : 'center'
        },
        {
          name : 'mailid',
          index : 'mailid'
        },
        {
          name : 'folderid',
          index : 'folderid'
        },
        {
          name : 'userid',
          index : 'userid'
        },
        {
          name : 'sendtime',
          index : 'sendtime'
        },
        {
          name : 'delflag',
          index : 'delflag'
        },
        {
          name : 'readflag',
          index : 'readflag'
        },
        {
          name : 'replyflag',
          index : 'replyflag'
        },
        {
          name : 'note',
          index : 'note'
        }
        ],
        rowNum : 15,
        rowList : [ 15, 20, 25, 30 ],
        pager : '#mailboxToolbar',
        // sortname : 'username',
        recordpos : 'right',
        viewrecords : true,
        sortorder : "desc",
        jsonReader : {
            repeatitems : false
        },
        multiselect : true,
        shrinkToFit : false,
        caption : "收件箱列表"
    });
    jQuery("#mailboxGird").jqGrid('navGrid', '#mailboxToolbar', {
        edit : false,
        add : false,
        del : false,
        search : false,
        refresh : true
    });
</script>
